package secondary_classes;
import java.util.ArrayList;
import java.util.Arrays;

import _runnable_classes.TradingEngine;


/**
 * @author Rohit
 *
 */
public class ReadOrders {
	
	//public static final String[] fields = {"#Instrument", "Date", "Time", "Record Type", "Price", "Volume", "Value", "Trans ID", "Bid ID", "Ask ID", "Bid/Ask"};
	public static final String[] requiredFields = {"#Instrument", "Date", "Time", "Record Type", "Price", "Volume", "Value", "Trans ID", "Bid ID", "Ask ID", "Bid/Ask"};
	public static final String[] optionalFields = {"Undisclosed Volume", "Qualifiers", "Entry Time", "Old Price", "Old Volume"};
	

	
	
	private int[] fieldOrder;
	private SortedList orders;
	private ArrayList<String> givenFields;
	
	/**
	 * @param filename Filename of the file to be read
	 * @param tradesOnly Whether or not to only accept trades
	 * @throws Exception
	 */
	public ReadOrders(String filename, boolean tradesOnly) throws Exception {
		
		// read in CSV file
		CsvFileReader cfr;
		try {
			cfr = new CsvFileReader(filename);
		} catch (Exception e) {
			throw new Exception("Could not find input file: " + filename);
		}
		
		int numLines = cfr.getNumLines();
		
		// check for all required fields
		System.out.print(TradingEngine.elapsedTimeString() + "  READING INPUT - Reading line " + 1 + " of " + numLines + "\r");
		ArrayList<String> allFields = new ArrayList<String>(Arrays.asList(requiredFields));
		allFields.addAll(Arrays.asList(optionalFields));
		this.givenFields = cfr.getLine(0);
		for (int i = 0; i < requiredFields.length; i++) {
			if (!givenFields.contains(requiredFields[i])) {
				throw new Exception("Could not find field: " + requiredFields[i]);
			}
		}
		
		// store fields in given order
		this.fieldOrder = new int[allFields.size()];
		for (int i = 0; i < fieldOrder.length; i++) {
			int pos = givenFields.indexOf(allFields.get(i));
			fieldOrder[i] = pos;
		}
		
		// create orders
		orders = new SortedList(new OrdersComparatorDateTime());
		
		String[] line = new String[givenFields.size()];
		int i = 1;
		while (i < numLines) {
			System.out.print(TradingEngine.elapsedTimeString() + "  READING INPUT - Reading line " + (i + 1) + " of " + numLines + "\r");
			// read in one record
			for (int j = 0; j < line.length; j++) {
				if (fieldOrder[j] != -1) {
					line[j] = cfr.getItem(i, fieldOrder[j]);
				} else {
					line[j] = "";
				}
			}
			try {
				// place values under correct headings
				Order newRecord = new Order(line);
				if (tradesOnly) {
					if (newRecord.getRecordType().equals("TRADE")) {
						orders.add(newRecord);
					}
				} else {
					orders.add(newRecord);
				}
				i++;
			} catch (Exception e) {
				throw new Exception("Order " + (orders.size() + 1) + ": " + e.getMessage());
			}
		}
		
		System.out.println(TradingEngine.elapsedTimeString() + "  READING INPUT - Done (Read " + i + " lines)                      ");
	
	}

	public Order getRecord(int i) {
		return orders.get(i);
	}
	
	public int size() {
		return orders.size();
	}
	
	public String toString() {
		String result = "ORDERS:\n";
		for (Order r : orders) {
			result += r.toString() + "\n";
		}
		return result;
	}

	public ArrayList<Order> getOrders() {
		return orders;
	}
	
	public ArrayList<String> getGivenFields() {
		return this.givenFields;
	}
	
}
